package com.caroil.caroilservice.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;

import com.caroil.caroilservice.R;
import com.caroil.caroilservice.view.TitleControlView;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.ycbjie.ycstatusbarlib.bar.StateAppBar;
import cn.ycbjie.ycstatusbarlib.utils.StatusBarUtils;

/**
 * Activity的基类
 */
public abstract class BaseActivity extends AbActivity {
    private Unbinder unbinder;
    public final static int REQUEST_CODE = 1001;
    private boolean isFullScreen = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            savedInstanceState.putParcelable("android:support:fragments", null);
        }
        super.onCreate(savedInstanceState);
        setContentView(bindLayout());
        ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
        loadMethod();

    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }

    protected abstract int bindLayout();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void loadMethod();

    @Override
    protected void onDestroy() {
        unbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onBackPressedSupport() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            pop();
        } else {
            ActivityCompat.finishAfterTransition(this);
        }
    }

    /**
     * 设置标题头
     *
     * @param view
     */
    public void initTitle(View view, String title, FragmentActivity activity) {
        TitleControlView t = new TitleControlView(view, () -> goBack(activity));
        t.setTitle(title);
    }


    /**
     * 返回上一页
     *
     * @param activity
     */
    public void goBack(FragmentActivity activity) {
        if (activity.getSupportFragmentManager().getBackStackEntryCount() > 1) {
            pop();
        } else {
            ActivityCompat.finishAfterTransition(activity);
        }
    }

    /**
     * 获取vm实例
     *
     * @return
     */
    public ViewModelProvider getViewModelProvider() {
        return new ViewModelProvider(this,
                ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()));
    }


}
